# -*- coding:utf-8 -*-
# --author： jingfeng 
# time: 2019/1/23


import gevent
import gevent.monkey

gevent.monkey.patch_all()



import requests
from fake_useragent import UserAgent
from lxml import etree

ua = UserAgent()
headers={'User-Agent':ua.random}

def get_teacher_info(numlist):
    print(numlist)
    for i in range:
        url = 'https://edu.csdn.net/lecturer?page=%s'%(str(i))

        # 得到 姓名 课程数   学生数   介绍地址

        response = requests.get(url, headers=headers)
        response.encoding = response.apparent_encoding
        html = response.text
        selector = etree.HTML(html)

       # print(html)

        name =selector.xpath('//dl[@class="lector_list"]/dd/ul/li[1]/a/text()')
        course_num = selector.xpath('//dl[@class="lector_list"]/dd/ul/li['
                                    '2]/span/text()')
        student_num = selector.xpath('//dl[@class="lector_list"]/dd/ul/li['
                                     '3]/span/text()')
        info_url = selector.xpath('//dl[@class="lector_list"]/dd/ul/li['
                                '1]/a/@href')

        infolist = []
        for url in info_url:
            print(url)
            response = requests.get(url, headers=headers)
            response.encoding = response.apparent_encoding
            html = response.text
            selector= etree.HTML(html)
            # 擅长领域
            field= selector.xpath('//div[@class="lector1_intro"]/p[1]/text('
                                  ')')[0]
            company = selector.xpath('//div[@class="lector1_intro"]/p[2]'
                                     '/a/text()')
            if not company:
                company = ''
                info = selector.xpath('//div[@class="lector1_intro"]/p['
                                      '2]/text()')[0]
            else:
                company = company[0]
                info = selector.xpath('//div[@class="lector1_intro"]/p['
                                      '3]/text()')[0]



            infolist.append({'field':field,'company':company,'info':info})



        return name, course_num, student_num, info_url,infolist





def save_info():
 pass




mylist = [x for x in range(1,57)]


xclist = [[],[],[],[],[],[],[],[],[],[]]

N =len(xclist)

for i in range(len(mylist)):
    xclist[i%N].append(mylist[i])  # 取余寻找列表，均匀分配数据

for list in xclist:
    print(list)


tasklist = []
for i in range(10):
    tasklist.append(gevent.spawn(get_teacher_info, xclist[i]))


gevent.joinall(tasklist)

